﻿using System;
using UnityEngine;

namespace SA
{
	// Token: 0x0200016F RID: 367
	[Serializable]
	public class FullBodyIKUnityChan : FullBodyIK
	{
		// Token: 0x060006B8 RID: 1720 RVA: 0x00003781 File Offset: 0x00001981
		public override bool _IsHiddenCustomEyes()
		{
			return true;
		}

		// Token: 0x060006B9 RID: 1721 RVA: 0x0003A414 File Offset: 0x00038614
		public override bool _PrepareCustomEyes(ref Quaternion headToLeftEyeRotation, ref Quaternion headToRightEyeRotation)
		{
			FullBodyIK.Bone bone = (this.headBones == null) ? null : this.headBones.head;
			FullBodyIK.Bone bone2 = (this.headBones == null) ? null : this.headBones.leftEye;
			FullBodyIK.Bone bone3 = (this.headBones == null) ? null : this.headBones.rightEye;
			if (bone != null && bone.transformIsAlive && bone2 != null && bone2.transformIsAlive && bone3 != null && bone3.transformIsAlive)
			{
				this._headToLeftEyeRotation = headToLeftEyeRotation;
				this._headToRightEyeRotation = headToRightEyeRotation;
				Vector3 b;
				FullBodyIK.SAFBIKMatMultVec(out b, ref this.internalValues.defaultRootBasis, ref FullBodyIKUnityChan._unityChan_leftEyeDefaultLocalPosition);
				Vector3 b2;
				FullBodyIK.SAFBIKMatMultVec(out b2, ref this.internalValues.defaultRootBasis, ref FullBodyIKUnityChan._unityChan_rightEyeDefaultLocalPosition);
				this._headBoneLossyScale = bone.transform.lossyScale;
				this._isHeadBoneLossyScaleFuzzyIdentity = FullBodyIK.IsFuzzy(this._headBoneLossyScale, Vector3.one, 1E-07f);
				if (!this._isHeadBoneLossyScaleFuzzyIdentity)
				{
					b = FullBodyIK.Scale(ref b, ref this._headBoneLossyScale);
					b2 = FullBodyIK.Scale(ref b2, ref this._headBoneLossyScale);
				}
				this._unityChan_leftEyeDefaultPosition = bone._defaultPosition + b;
				this._unityChan_rightEyeDefaultPosition = bone._defaultPosition + b2;
			}
			return true;
		}

		// Token: 0x060006BA RID: 1722 RVA: 0x0003A56C File Offset: 0x0003876C
		public override void _ResetCustomEyes()
		{
			FullBodyIK.Bone bone = (this.headBones == null) ? null : this.headBones.neck;
			FullBodyIK.Bone bone2 = (this.headBones == null) ? null : this.headBones.head;
			FullBodyIK.Bone bone3 = (this.headBones == null) ? null : this.headBones.leftEye;
			FullBodyIK.Bone bone4 = (this.headBones == null) ? null : this.headBones.rightEye;
			if (bone != null && bone.transformIsAlive && bone2 != null && bone2.transformIsAlive && bone3 != null && bone3.transformIsAlive && bone4 != null && bone4.transformIsAlive)
			{
				Quaternion worldRotation = bone.worldRotation;
				Vector3 worldPosition = bone.worldPosition;
				FullBodyIK.Matrix3x3 matrix3x;
				FullBodyIK.SAFBIKMatSetRotMultInv1(out matrix3x, ref worldRotation, ref bone._defaultRotation);
				Vector3 vector;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out vector, ref matrix3x, ref bone2._defaultPosition, ref bone._defaultPosition, ref worldPosition);
				Quaternion worldRotation2 = bone2.worldRotation;
				FullBodyIK.Matrix3x3 matrix3x2;
				FullBodyIK.SAFBIKMatSetRotMultInv1(out matrix3x2, ref worldRotation2, ref bone2._defaultRotation);
				Vector3 worldPosition2;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out worldPosition2, ref matrix3x2, ref bone3._defaultPosition, ref bone2._defaultPosition, ref vector);
				bone3.worldPosition = worldPosition2;
				Quaternion worldRotation3;
				FullBodyIK.SAFBIKQuatMult(out worldRotation3, ref worldRotation2, ref this._headToLeftEyeRotation);
				bone3.worldRotation = worldRotation3;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out worldPosition2, ref matrix3x2, ref bone4._defaultPosition, ref bone2._defaultPosition, ref vector);
				bone4.worldPosition = worldPosition2;
				FullBodyIK.SAFBIKQuatMult(out worldRotation3, ref worldRotation2, ref this._headToRightEyeRotation);
				bone4.worldRotation = worldRotation3;
			}
		}

		// Token: 0x060006BB RID: 1723 RVA: 0x0003A6EC File Offset: 0x000388EC
		public override void _SolveCustomEyes(ref FullBodyIK.Matrix3x3 neckBasis, ref FullBodyIK.Matrix3x3 headBasis, ref FullBodyIK.Matrix3x3 headBaseBasis)
		{
			FullBodyIK.Bone bone = (this.headBones == null) ? null : this.headBones.neck;
			FullBodyIK.Bone bone2 = (this.headBones == null) ? null : this.headBones.head;
			FullBodyIK.Bone bone3 = (this.headBones == null) ? null : this.headBones.leftEye;
			FullBodyIK.Bone bone4 = (this.headBones == null) ? null : this.headBones.rightEye;
			FullBodyIK.Effector effector = (this.headEffectors == null) ? null : this.headEffectors.eyes;
			if (bone != null && bone.transformIsAlive && bone2 != null && bone2.transformIsAlive && bone3 != null && bone3.transformIsAlive && bone4 != null && bone4.transformIsAlive && effector != null)
			{
				Vector3 worldPosition = bone.worldPosition;
				Vector3 vector;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out vector, ref neckBasis, ref bone2._defaultPosition, ref bone._defaultPosition, ref worldPosition);
				Vector3 b;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out b, ref headBasis, ref effector._defaultPosition, ref bone2._defaultPosition, ref vector);
				Vector3 vector2 = effector.worldPosition - b;
				FullBodyIK.Matrix3x3 matrix3x = headBaseBasis;
				FullBodyIK.Matrix3x3 matrix3x2 = headBaseBasis;
				FullBodyIK.SAFBIKMatMultVecInv(out vector2, ref headBaseBasis, ref vector2);
				if (!FullBodyIK.SAFBIKVecNormalize(ref vector2))
				{
					vector2 = new Vector3(0f, 0f, 1f);
				}
				if (effector.positionWeight < 0.9999999f)
				{
					Vector3 vector3 = Vector3.Lerp(new Vector3(0f, 0f, 1f), vector2, effector.positionWeight);
					if (FullBodyIK.SAFBIKVecNormalize(ref vector3))
					{
						vector2 = vector3;
					}
				}
				FullBodyIK._LimitXY_Square(ref vector2, FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta, FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta, FullBodyIKUnityChan._unityChan_eyesVertLimitTheta, FullBodyIKUnityChan._unityChan_eyesVertLimitTheta);
				float num = vector2.x * 0.796f;
				if (num < -FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta)
				{
					num = -FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta;
				}
				else if (num > FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta)
				{
					num = FullBodyIKUnityChan._unityChan_eyesHorzLimitTheta;
				}
				vector2.x *= 0.796f;
				vector2.y *= 0.28f;
				Vector3 vector4 = vector2;
				Vector3 vector5 = vector2;
				if (vector2.x >= 0f)
				{
					vector4.x *= 0.065f;
					vector5.x *= 0.096f;
				}
				else
				{
					vector4.x *= 0.096f;
					vector5.x *= 0.065f;
				}
				FullBodyIK.SAFBIKVecNormalize2(ref vector4, ref vector5);
				FullBodyIK.SAFBIKMatMultVec(out vector4, ref headBaseBasis, ref vector4);
				FullBodyIK.SAFBIKMatMultVec(out vector5, ref headBaseBasis, ref vector5);
				float num2 = (num < 0f) ? 0.0063f : 0.0063f;
				float num3 = (num < 0f) ? 0.0063f : 0.0063f;
				FullBodyIK.SAFBIKComputeBasisLockZ(out matrix3x, ref headBasis.column0, ref headBasis.column1, ref vector4);
				FullBodyIK.SAFBIKComputeBasisLockZ(out matrix3x2, ref headBasis.column0, ref headBasis.column1, ref vector5);
				Vector3 a = headBaseBasis.column0 * (num2 * num);
				Vector3 a2 = headBaseBasis.column0 * (num3 * num);
				if (!this._isHeadBoneLossyScaleFuzzyIdentity)
				{
					a = FullBodyIK.Scale(ref a, ref this._headBoneLossyScale);
					a2 = FullBodyIK.Scale(ref a2, ref this._headBoneLossyScale);
				}
				Vector3 b2;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out b2, ref headBasis, ref this._unityChan_leftEyeDefaultPosition, ref bone2._defaultPosition, ref vector);
				a += b2;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out b2, ref headBasis, ref this._unityChan_rightEyeDefaultPosition, ref bone2._defaultPosition, ref vector);
				a2 += b2;
				FullBodyIK.Matrix3x3 matrix3x3;
				FullBodyIK.SAFBIKMatMult(out matrix3x3, ref matrix3x, ref this.internalValues.defaultRootBasisInv);
				FullBodyIK.Matrix3x3 matrix3x4;
				FullBodyIK.SAFBIKMatMult(out matrix3x4, ref matrix3x2, ref this.internalValues.defaultRootBasisInv);
				Vector3 worldPosition2;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out worldPosition2, ref matrix3x3, ref bone3._defaultPosition, ref this._unityChan_leftEyeDefaultPosition, ref a);
				bone3.worldPosition = worldPosition2;
				Quaternion worldRotation;
				FullBodyIK.SAFBIKMatMultGetRot(out worldRotation, ref matrix3x, ref bone3._baseToWorldBasis);
				bone3.worldRotation = worldRotation;
				FullBodyIK.SAFBIKMatMultVecPreSubAdd(out worldPosition2, ref matrix3x4, ref bone4._defaultPosition, ref this._unityChan_rightEyeDefaultPosition, ref a2);
				bone4.worldPosition = worldPosition2;
				FullBodyIK.SAFBIKMatMultGetRot(out worldRotation, ref matrix3x2, ref bone4._baseToWorldBasis);
				bone4.worldRotation = worldRotation;
			}
		}

		// Token: 0x04000AED RID: 2797
		private Vector3 _headBoneLossyScale = Vector3.one;

		// Token: 0x04000AEE RID: 2798
		private bool _isHeadBoneLossyScaleFuzzyIdentity = true;

		// Token: 0x04000AEF RID: 2799
		private Quaternion _headToLeftEyeRotation = Quaternion.identity;

		// Token: 0x04000AF0 RID: 2800
		private Quaternion _headToRightEyeRotation = Quaternion.identity;

		// Token: 0x04000AF1 RID: 2801
		private Vector3 _unityChan_leftEyeDefaultPosition = Vector3.zero;

		// Token: 0x04000AF2 RID: 2802
		private Vector3 _unityChan_rightEyeDefaultPosition = Vector3.zero;

		// Token: 0x04000AF3 RID: 2803
		private static Vector3 _unityChan_leftEyeDefaultLocalPosition = new Vector3(-0.018530998f, 0.048524f, 0.027681999f);

		// Token: 0x04000AF4 RID: 2804
		private static Vector3 _unityChan_rightEyeDefaultLocalPosition = new Vector3(0.018530998f, 0.048524f, 0.027681999f);

		// Token: 0x04000AF5 RID: 2805
		private static readonly float _unityChan_eyesHorzLimitTheta = Mathf.Sin(0.6981317f);

		// Token: 0x04000AF6 RID: 2806
		private static readonly float _unityChan_eyesVertLimitTheta = Mathf.Sin(0.07853982f);

		// Token: 0x04000AF7 RID: 2807
		private const float _unityChan_eyesYawRate = 0.796f;

		// Token: 0x04000AF8 RID: 2808
		private const float _unityChan_eyesPitchRate = 0.28f;

		// Token: 0x04000AF9 RID: 2809
		private const float _unityChan_eyesYawOuterRate = 0.096f;

		// Token: 0x04000AFA RID: 2810
		private const float _unityChan_eyesYawInnerRate = 0.065f;

		// Token: 0x04000AFB RID: 2811
		private const float _unityChan_eyesMoveXInnerRate = 0.0063f;

		// Token: 0x04000AFC RID: 2812
		private const float _unityChan_eyesMoveXOuterRate = 0.0063f;
	}
}
